package com.uroad.liaoningpos.common;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.boc.aidl.deviceService.AidlDeviceService;
import com.newland.paymentbc.aidl.IPaymentService;
import com.uroad.liaoningpos.R;
import com.uroad.lib.activity.BaseUroadActivity;
import com.uroad.lib.util.log.LogUtil;

/**
 * @创建者 wlw
 * @创建时间 2019/4/2 15:43
 * @描述 BaseActivity
 */
public class BaseActivity extends BaseUroadActivity {
    private static String TAG = "BaseActivity";
    protected static AidlDeviceService deviceService;
    protected static IPaymentService infoService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBar();
    }

    private void initTitleBar() {
        setLeftBtn(R.mipmap.ic_back);
        setTitleBackgroundColor(getResources().getColor(R.color.main_title_blue));
        Button leftButton = getLeftButton();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) leftButton.getLayoutParams();
        params.setMargins(getResources().getDimensionPixelOffset(R.dimen.space_10), 0, 0, 0);
        leftButton.setLayoutParams(params);
    }

    protected void bindDeviceService() {
        if (deviceService == null) {
            Intent intent = new Intent();
            intent.setPackage("com.boc.spos.service");
            intent.setAction("com.boc.DeviceService");

            bindService(intent, serviceConnection, BIND_AUTO_CREATE);
            LogUtil.i(TAG, "---DeviceService开始绑定服务---");
        } else {
            LogUtil.i(TAG, "---DeviceService服务已经启动---");
        }
    }

    protected void bindInfoService() {
        if (infoService == null) {
            Intent intent = new Intent();
            intent.setPackage(GlobalData.YADA_PKG);
            intent.setAction(GlobalData.YADA_SERVICE);

            bindService(intent, infoserviceConnection, BIND_AUTO_CREATE);
            LogUtil.i(TAG, "---InfoService开始绑定服务---");
        } else {
            LogUtil.i(TAG, "---InfoService服务已经启动---");
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isFinishing()) {
            if (GlobalData.isConnectionService) {
                unBindDeviceService();
            }
            if (GlobalData.isConnectionInfoService) {
                unBindInfoService();
            }
        }
    }

    /**
     * 解绑服务
     */
    public void unBindDeviceService() {
        if (deviceService != null) {
            try {
                unbindService(serviceConnection);
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.i(TAG, "服务解绑异常：");
            } finally {
                deviceService = null;
                //                btnStateToWaitingInit();
                GlobalData.isConnectionService = false;
            }

        } else {
            LogUtil.i(TAG, "---DeviceService服务未启动---");
        }
    }
    /**
     * 解绑服务
     */
    public void unBindInfoService() {
        if (infoService != null) {
            try {
                unbindService(infoserviceConnection);
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.i(TAG, "服务解绑异常：");
            } finally {
                infoService = null;
                GlobalData.isConnectionInfoService = false;
            }
        } else {
            LogUtil.i(TAG, "---InfoService服务未启动---");
        }
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogUtil.i(TAG, "deviceService服务断开连接");
            deviceService = null;
            //            btnStateToWaitingInit();
            GlobalData.isConnectionService = false;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            deviceService = AidlDeviceService.Stub.asInterface(service);
            LogUtil.i(TAG, "deviceService服务连接成功");
            GlobalData.isConnectionService = true;
            //            btnStateToWaitingConn();
            bindDeviceServiceSuccess();
        }
    };
    private ServiceConnection infoserviceConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogUtil.i(TAG, "infoService服务断开连接");
            infoService = null;
            //            btnStateToWaitingInit();
            GlobalData.isConnectionInfoService = false;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            infoService = IPaymentService.Stub.asInterface(service);
            LogUtil.i(TAG, "infoService服务连接成功");
            GlobalData.isConnectionInfoService = true;
            //            btnStateToWaitingConn();
            bindInfoServiceSuccess();
        }
    };

    protected void bindInfoServiceSuccess() {

    }

    /**
     * 绑定service成功
     */
    protected void bindDeviceServiceSuccess() {

    }
}
